package com.sctvcloud.qingchuan.ui.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.flyco.tablayout.SlidingTabLayout;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.sctvcloud.qingchuan.R;
import com.sctvcloud.qingchuan.application.Cache;
import com.sctvcloud.qingchuan.application.Constances;
import com.sctvcloud.qingchuan.base.BaseActivity;
import com.sctvcloud.qingchuan.beans.PoliticsAccountBean;
import com.sctvcloud.qingchuan.beans.PoliticsAccountTag;
import com.sctvcloud.qingchuan.http.AbsNetCallBack;
import com.sctvcloud.qingchuan.http.NetUtils;
import com.sctvcloud.qingchuan.ui.adapter.PoliticsTagPagerAdapter;
import com.sctvcloud.qingchuan.ui.fragment.ColumnBaseFragment;
import com.sctvcloud.qingchuan.ui.utils.TitleUtils;
import com.sctvcloud.qingchuan.utils.ListTypeUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 选择问政对象
 */
public class PoliticsChooseObjActivity extends BaseActivity {

    @BindView(R.id.home_tablay)
    protected SlidingTabLayout home_tablay;
    @BindView(R.id.home_content_vp)
    protected ViewPager home_content_vp;

    @BindView(R.id.home_top_lay_tab)
    protected ViewGroup tabLay;
    @BindView(R.id.home_content_ll)
    protected LinearLayout content_ll;
    @BindView(R.id.title_top_edit_topic)
    protected CustomFontTextView confirm;

    public static final String POLITICS_OBJ_ID = "politics_obj_id";
    public static final String POLITICS_OBJ_NAME = "politics_obj_name";
    private PoliticsTagPagerAdapter adapter;
    private PoliticsAccountBean politicsObj;
    private long lastId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new TitleUtils(this).useAndSetTxTitle("回音壁对象");
        lastId = getIntent().getLongExtra(POLITICS_OBJ_ID, 0);
        if (lastId > 0) {
            politicsObj = new PoliticsAccountBean();
            politicsObj.setAuthorId(lastId);
        }
        initView();
        getData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void initInject() {
        setContentView(R.layout.activity_choose_politcs_obj);
        ButterKnife.bind(this);
    }


    private void initView() {
        confirm.setVisibility(View.VISIBLE);
        confirm.setText("完成");

    }


    @Override
    public void onResume() {
        super.onResume();
    }


    private void getData() {

        NetUtils.getNetAdapter().toGetPoliticsAccountTag(getOwnerName(), Constances.TENANT_ID, new AbsNetCallBack<String>(String.class) {
            @Override
            public void onSucStr(String singleResult) {
                if (!TextUtils.isEmpty(singleResult)) {
                    List<PoliticsAccountTag> list = gson.fromJson(singleResult, ListTypeUtils.PoliticsAccountTag());
                    initPoliticsTag(list);
                }

            }

            @Override
            public void onError(Throwable ex, String errorMsg) {
                if (TextUtils.isEmpty(errorMsg)) {
                    toast("获取回音壁对象失败");
                } else {
                    toast(errorMsg);
                }

                closeProgress();
            }

            @Override
            public void onEnd() {
                closeProgress();
                Cache.getInstance().setUploadReb(false);
            }

        });
    }

    private void initPoliticsTag(List<PoliticsAccountTag> list) {
        if (adapter == null) {
            adapter = new PoliticsTagPagerAdapter(this, getSupportFragmentManager(), list);
            setAdapterTopHeiht(true);
            adapter.setLastSelectedId(lastId);
            home_content_vp.setAdapter(adapter);
            home_content_vp.setOffscreenPageLimit(1);
            home_content_vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                }

                @Override
                public void onPageSelected(int position) {
                    int count = adapter.getCount();
                    for (int i = 0; i < count; i++) {
                        ColumnBaseFragment fragment = adapter.getFragmentByPos(i);
                        if (position == i) {
                            fragment.onHiddenChanged(false);
                        } else {
                            fragment.onHiddenChanged(true);
                        }
                    }
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
            home_tablay.setViewPager(home_content_vp);
        } else {
            setAdapterTopHeiht(true);
            adapter.setData(list);
        }

    }

    private void setAdapterTopHeiht(boolean isNewInstance) {
        if (isNewInstance) {
            adapter.setTopHeight(tabLay.getHeight());
        }
    }

    @OnClick({R.id.title_top_edit_topic})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_top_edit_topic:
                if (null != politicsObj) {
                    Intent intent = new Intent();
                    intent.putExtra(POLITICS_OBJ_ID, politicsObj.getAuthorId());
                    intent.putExtra(POLITICS_OBJ_NAME, politicsObj.getUserName());
                    setResult(RESULT_OK, intent);
                    finish();
                } else {
                    toast("请选择回音壁对象");
                }
                break;
        }
    }

    /**
     * 选择问政对象，赋值
     *
     * @param account
     */
    public void setPoliticsObj(PoliticsAccountBean account) {
        this.politicsObj = account;

    }

    public PoliticsAccountBean getPoliticsObj() {
        return politicsObj;
    }
}
